<?php
/*
 * 手机客户端 秀场展品列表接口
 * 传入参数 
 * 2013-12-011
 */
include "../inc/public_header.php";
include "../inc/MiniaturePro_new.class.php";
include "../func/user.php";				//用户信息处理函数
include "../func/pic.php";					//图片处理函数

$reJson = array();//构建返回值
 
$page			= $_REQUEST["page"]; 			//页数，第一页为0
$pagesize		= $_REQUEST["pagesize"];		//每页记录数
$xuexiao_id	    = $_REQUEST["cid"];		//学院
$panelWidth		= $_REQUEST["panelWidth"];		//屏幕宽度
$hd_id			= $_REQUEST["hdId"];			//活动主键
$groupFilter	= $_REQUEST["groupFilter"];		//过滤条件key
$childFilter	= $_REQUEST["childFilter"];		//过滤条件value
$showSF			= $_REQUEST["showSF"];			//是否返回过滤,排序信息,  1:返回; 0:不返回
$version		= $_REQUEST["ver"];				//版本号


$groupFilterArr = explode(",",$groupFilter);	//转换为数组  sort
$childFilterArr = explode(",",$childFilter);	//转换为数组




$name = "name";
$value= "value";
$child="child";	
		
if($version>=1.7||strlen($version)>3){
	if($panelWidth<480){
		$imgWidth  = 50;
	}else if($panelWidth>=480 && $panelWidth<=600){
		$imgWidth = 100;
	}else {
		$imgWidth = 150;
	}
} else{
	if($panelWidth<480){
		$imgWidth  = 100;
	}else if($panelWidth>=480 && $panelWidth<=600){
		$imgWidth = 200;
	}else {
		$imgWidth = 300;
	}
}	

	 
if(!isset($page)){
	$page = 0;
}
if(!isset($pagesize)){
	$pagesize = 20; //默认每页200条
}

$reJson["success"] = "1";

/**
//获取作品总数量
$sql ="select count(0) as ct
		from 
				new_weituo_zuopin a	
				left join new_weituo_person d on a.uid=d.uid
		where
				a.dr='0'
				and a.aflag='1'
		";
if($cid){
	$sql = $sql." and d.xuexiao_id='".$cid."' ";
}



$db->query($sql);
$db->next_record();
$reJson["count"] = $db->f("ct"); //设置作品总数量（不分页）
**/

$sql ="SELECT id FROM `new_weituo_huodong` where state=1 and id='".$hd_id."'";
$db->query($sql);
if($db->next_record()){
	$canUpload="1";
}else{
	$canUpload="0";
}
$reJson["canUpload"] = $canUpload;
$reJson["wapUrl"] = "http://www.baidu.com";
$sql ="select a.id
		from 
				new_weituo_zuopin a	
				left join new_weituo_person d on a.uid=d.uid
		where
				a.dr='0'
				and a.aflag='1'
		";


if(isset($hd_id) && $hd_id!=""){
	$sql = $sql." and a.hd_id='".$hd_id."' ";
}
if ($xuexiao_id){
	$sql = $sql."and d.xuexiao_id='".$xuexiao_id."'";
}
$sortValue = ""; //排序类型值
if(count($groupFilterArr)>0){
	for($i=0;$i<count($groupFilterArr);$i++){
		if(!$groupFilterArr[$i] || !$childFilterArr[$i]){
			continue;
		}
		if($groupFilterArr[$i]=="sort"){
			//获取排序标识
			$sortValue = $childFilterArr[$i];
		}else{
			$sql = $sql." and ".$groupFilterArr[$i]."='".$childFilterArr[$i]."'";
		}
	}
}




$idArr=$db->queryArray($sql);
$listCount = count($idArr); //记录数
$reJson["count"] =(string)$listCount;
//echo json_encode($idArr);
$ids = "";
for($i=0;$i<$listCount;$i++){
	if($i>0){
		$ids = $ids.",";
	}
	$ids = $ids.$idArr[$i][0];
	
}
if(!$ids){
	$ids = "0";
}
//echo $ids;
//echo $ids;
if(count($groupFilterArr)!=count($childFilterArr)){
	echo json_encode(array("success"=>"0","msg"=>"groupFilter与childFilter数量不等"));
	die;
}


//作品信息列表
$sql ="select t1.id,t1.uid,t1.aname,t1.gz_count,t1.ll_count,t1.pl_count,t1.czln,t1.img_path,t1.img_id,t1.type_id,t1.zan_count
			  ,t2.mini_img_id,t2.mini_img_width,t2.mini_img_height,t2.mini_img_aname
		from 
				(select id,uid,aname,gz_count,ll_count,pl_count,czln,img_path,img_id,type_id,zan_count,ctime from new_weituo_zuopin where dr=0 and aflag=1 and id in (".$ids.")) t1
				LEFT JOIN (select img_id,id as mini_img_id,awidth as mini_img_width,aheight as mini_img_height,aname as mini_img_aname from new_weituo_pic_size where awidth='".$imgWidth."' ) t2 on t1.img_id=t2.img_id
				LEFT JOIN new_weituo_person t3 on t1.uid=t3.uid
		";

$sql .= " group by t1.id  order by t1.ctime desc limit ".($page * $pagesize).",".$pagesize;

$db->query($sql);
$worksArr = array();
$miniature = new MiniaturePro();	//构建缩略图处理类
//echo $sql;
while($db->next_record()){
	//处理生成缩略图
	$imgInfos = pic_resize(
							 $miniature
							,$db->f("img_path")
							,$db->f("mini_img_aname")
							,$imgWidth
							,$db->f("img_id")
							,$db->f("mini_img_id")
							,$db->f("mini_img_width")
							,$db->f("mini_img_height"));
	if($imgInfos["success"]=="0"){
		//图片有问题，无法显示
		continue;
	}
	$worksArr[] = array(
		"title"=>$db->f("aname")							//作品标题
		,"worksId"=>$db->f("id")							//作品id
		,"imgUrl"=>$imgInfos["mini_img_url"]				//图片
		,"authorId"=>$db->f("uid")							//作者id
		,"praise"=>$db->f("zan_count")						//赞
		,"comment"=>$db->f("pl_count")						//评论
		,"imgWidth"=>(string)$imgInfos["mini_img_width"]	//图片宽度
		,"imgHeight"=>(string)$imgInfos["mini_img_height"]	//图片高度
	);
}
//echo json_encode($worksArr);
$reJson["list"]=$worksArr;



if($showSF && intval($showSF,10)==1){


//获取分类信息
if (!$xuexiao_id){
if($hd_id==3){
$sql="
	select 
			distinct a.type_id as type
			,b.aname as name
			,b.aindex
		from 
			new_weituo_zuopin a 
			left join new_weituo_bsxfenlei b on a.type_id=b.id 
		where 
			a.dr='0' 
			and a.aflag='1'
			and not a.type_id is null
			and a.hd_id='3'
			and a.type_id!='0'
		order by b.aindex asc
";
}else if($hd_id==2){
$sql="
	select 
			distinct a.type_id as type
			,b.aname as name
			,b.aindex
		from 
			new_weituo_zuopin a 
			left join new_weituo_fenlei b on a.type_id=b.id 
		where 
			a.dr='0' 
			and a.aflag='1'
			and not a.type_id is null
			and a.hd_id='2' 
		order by b.aindex asc
";
}else if($hd_id==1){
$sql="
	select 
			distinct a.type_id as type
			,b.aname as name
			,b.aindex
		from 
			new_weituo_zuopin a 
			left join new_weituo_fenlei b on a.type_id=b.id 
		where 
			a.dr='0' 
			and a.aflag='1'
			and not a.type_id is null
			and a.hd_id='1' 
		order by b.aindex asc
";
}
else if($hd_id==7){
$sql="
	select 
			distinct a.type_id as type
			,b.aname as name
			,b.aindex
		from 
			new_weituo_zuopin a 
			left join new_weituo_fenlei b on a.type_id=b.id 
		where 
			a.dr='0' 
			and a.aflag='1'
			and not a.type_id is null
			and a.hd_id='7' 
		order by b.aindex asc
";
}else if($hd_id==4){
$sql="
	select 
			distinct a.type_id as type
			,b.aname as name
			,b.aindex
		from 
			new_weituo_zuopin a 
			left join new_weituo_zbfenlei b on a.type_id=b.id 
		where 
			a.dr='0' 
			and a.aflag='1'
			and not a.type_id is null
			and a.hd_id='4' 
		order by b.aindex asc
";
}
}else{
$sql="
	select 
			distinct a.type_id as type
			,b.aname as name
			,b.aindex
		from 
			new_weituo_zuopin a 
			left join new_weituo_fenlei b on a.type_id=b.id 
			left join new_weituo_person d on a.uid=d.uid
		where 
			a.dr='0' 
			and a.aflag='1'
			and not a.type_id is null
			and a.hd_id=1 and d.xuexiao_id='".$xuexiao_id."'
		order by b.aindex asc
";
}
$db->query($sql);
$typeArr = array();
$tempArr = array();

	$tempArr = array(
	"name"=>"全部"											
	,"value"=>""										
);

while($db->next_record()){
	
	$typeArr[] = array(
		"name"=>(string)$db->f("name")			//分类名称
		,"value"=>(string)$db->f("type")		//分类id
	);
	
}
if ($typeArr){
array_unshift($typeArr,$tempArr);
}
$filter = array();
if(count($typeArr)>0){
$filter[] = array(
		(string)$name=>"分类"
		,(string)$value=>"type_id"
		,(string)$child=>$typeArr
	);
}		
//放入分类信息
$reJson["filter"] = $filter;
}
echo json_encode($reJson);
?>